package com.group.yqxxb.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.group.yqxxb.Exception.MyException;
import com.group.yqxxb.entity.User;
import com.group.yqxxb.response.R;
import com.group.yqxxb.request.UserCard;
import com.group.yqxxb.service.MessageService;
import com.group.yqxxb.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    private static final Logger LOG = LoggerFactory.getLogger(MessageService.class);
    @Autowired
    UserService userService;

    /**
     * 用户注册
     */
    @PostMapping("/create")
    public R UserCreate(@ModelAttribute("user") User user){
        try{
            JSONObject jsonObject = userService.InsertUser(user);
            JSONObject info = jsonObject.getJSONObject("info");
            return R.success(info);
        }catch (Exception e){
            return R.error("用户注册失败或已存在用户");
        }
    }

    /**
     * 更新token
     */
    @PostMapping("/updateToken")
    public R UpdateToken(@RequestParam("accid") String accid,
                         @RequestParam(value = "token",required = false) String token){
        JSONObject jsonObject;
        try{
            jsonObject = userService.UpdateToken(accid, token);
            return R.success(token);
        }catch (Exception e){
            return R.error(e);
        }
    }

    /**
     *重置token
     * @param accid
     * @return
     */
    @PostMapping("/refreshToken")
    public R RefreshToken(@RequestParam("accid") String accid){
        JSONObject jsonObject;
        try {
            jsonObject = userService.RefreshToken(accid);
            return R.success(jsonObject);
        }catch (Exception e){
            return R.error(e);
        }
    }

    /**
     * 更新用户名片
     * @param userCard
     * @return
     */
    @PostMapping("/updateUinfo")
    public R UpdateUinfo(@RequestBody UserCard userCard){
        Boolean flag;
        try{
            flag = userService.UpdateUinfo(userCard);
        }catch (Exception e){
            return R.error(e);
        }
        return R.success(userCard.getAccid());
    }

    /**
     * 设置桌面端在线时，移动端是否需要推送
     * @param accid
     * @param donnopOpen
     * @return
     */
    @PostMapping("/setDonnop")
    public R SetDonnop(@RequestParam("accid") String accid,
                       @RequestParam("donnopOpen") String donnopOpen){
        Boolean result;
        try{
            result = userService.SetDonnop(accid,donnopOpen);
        }catch (Exception e){
            return R.error(e);
        }
        if (donnopOpen.equals("true")) {
            return R.success("桌面端在线时，移动端不推送");
        }else{
            return R.success("桌面端在线时，移动端推送");
        }
    }

    /**
     * 登录 返回token，前端使用token登录网易云信
     * @param accid
     * @return
     */
    @PostMapping("/login")
    public R Login(@RequestParam("accid") String accid){
        String token;
        try{
            token = userService.Login(accid);
        }catch (Exception e){
            return R.error(e);
        }
        if(ObjectUtils.isEmpty(token)||token==null){
            LOG.error("token为空，账号未注册");
            return R.error("账号未注册");
        }
        return R.success(token);
    }


    /**
     * 设置全局禁言
     * @param accid
     * @param mute
     * @return
     */
    @PostMapping("/mute")
    public R Mute(@RequestParam("accid") String accid,
                  @RequestParam("mute") Boolean mute){
        try{
            userService.Mute(accid,mute);
            if (mute == true){
                return R.success("全局禁言");
            }else{
                return R.success("取消全局禁言");
            }
        }catch (Exception e){
            return R.error(e);
        }

    }

    /**
     * 获取名片
     * @param accidss
     * @return
     */
    @PostMapping("/getUinfos")
    public R GetUinfos(@RequestBody JSONObject accidss){
        JSONArray jsonArray;
        //获取jsonObject中的json数组
        JSONArray accids = accidss.getJSONArray("accids");
        try{
            jsonArray = userService.GetUinfos(accids);
        }catch (Exception e){
            return R.error(e);
        }
        return R.success(jsonArray);
    }



    @PostMapping("/search")
    public R SearchInfo(@Param("accid")String accid) throws Exception {
        User user = userService.SearchInfo(accid);
        if (user==null){
            return R.error("用户不存在");
        }else {
            return R.success(user);
        }
    }
}

